package com.blue.gateway.paycore.wxpay.bill;

import com.alibaba.fastjson.JSON;
import com.blue.gateway.common.Request;
import com.blue.gateway.config.BaseChannelConfig;
import com.blue.gateway.dto.Result;
import com.blue.gateway.dto.ResultWrapper;
import com.blue.gateway.enums.PayChannelEnum;
import com.blue.gateway.annotation.Channel;
import com.blue.gateway.common.request.BillRequest;
import com.blue.gateway.common.response.BillResponse;
import com.blue.gateway.config.WxConfig;
import com.blue.gateway.paycore.wxpay.WxPayConstants;
import com.blue.gateway.paycore.wxpay.pay.WxPayService;
import com.blue.gateway.enums.ResultCodeEnum;
import com.blue.gateway.enums.ServiceChannelEnum;
import com.blue.gateway.utils.RandomUtil;
import com.blue.gateway.utils.XmlUtil;
import com.blue.gateway.utils.http.HttpClientsUtil;
import com.blue.gateway.utils.sign.WxPaySignature;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author xiaoyiming
 * @date 2020/5/3 19:15
 * @depiction 微信支付接口
 * 下载交易账单:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_6&index=6
 */
@Slf4j
@Channel(PayChannel = PayChannelEnum.WX, ServiceChannel = ServiceChannelEnum.BILL_DOWNLOAD)
public class WxPayBillService extends WxPayService {

    @Override
    public Result validate(Request request) {
        BillRequest billRequest = (BillRequest) request;
        if (StringUtils.isEmpty(billRequest.getBillDate())) {
            return ResultWrapper.fail(ResultCodeEnum.PARAM_ILLEGAL);
        }
        return ResultWrapper.success();
    }


    @Override
    public BaseChannelConfig getConfig(Request request) {
        return super.getConfig(request);
    }

    @Override
    public String generateParams(Request request, BaseChannelConfig channelConfig) {
        BillRequest billRequest = (BillRequest) request;
        WxConfig wxConfig = (WxConfig) channelConfig;

        SortedMap<String, String> params = new TreeMap<>();
        params.put("appid", wxConfig.getAppId());
        params.put("mch_id", wxConfig.getMchId());
        params.put("nonce_str", RandomUtil.getRandomStr());
        params.put("bill_date", billRequest.getBillDate());
        params.put("bill_type", "ALL");
        if (StringUtils.isNotEmpty(billRequest.getBillType())) {
            params.put("bill_type", billRequest.getBillType());
        }

        String sign = WxPaySignature.sign(params, wxConfig.getParterKey());
        params.put("sign", sign);
        String xmlString = XmlUtil.mapToXML(params);

        log.info("xmlString:{}", xmlString);
        return xmlString;
    }

    @Override
    public String execute(String requestParams, BaseChannelConfig channelConfig) {
        // 发送请求
        String executeResult = null;
        try {
            executeResult = HttpClientsUtil.httpClientPost(WxPayConstants.WX_DOWNLOAD_BILL_GATEWAY, requestParams);
        } catch (Exception e) {
            // do nothing
        }
        return executeResult;
    }

    //微信直接返回账单数据
    @Override
    public Result analysisResult(String executeResult) {
        String jsonData = JSON.toJSONString(executeResult);
        BillResponse payResponse = new BillResponse();
        payResponse.setData(jsonData);
        return ResultWrapper.success(payResponse);
    }
}
